/* 
 * Copyright [2017] [Alex/LiBo(libo2dev.aliyun.com/alex.link@foxmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.sdkhttp.domain;

import com.alibaba.fastjson.JSONObject;
import com.hyts.sdkhttp.error.SdkUploadError;
import com.hyts.sdkhttp.util.StringUtil;

import java.io.File;
import java.nio.file.Paths;

/**  
 * @Title SdkUpload.java  
 * @Package com.hyts.http.domain  
 * @Description sdk上传文件操作请求业务实现模型类对象  
 * @author LiBo/Alex  
 * @date 2018年3月13日  
 * @version V1.0  
 */
public class SdkUploadRequest extends SdkRequest{

	/**  
	 * @Fields field:field: 必选：filePath文件路径
	 */ 
	private String filePath;
	
	/**  
	 * @Fields field:field: 可选：fileSize文件大小：自动生成  
	 */ 
	private Long fileSize;
	
	/**  
	 * @Fields field:field:传递文件的参数名称:默认filename  
	 */ 
	private String fileKeyName;
	
	/**  
	 * @return fileKeyName  
	 */
	public String getFileKeyName() {
		return fileKeyName;
	}

	/**  
	 * @Fields field:field:文件路径  
	 */ 
	private String fileName;
	
	/**  
	 * @Fields field:field: 可选：fileExtName文件扩展名：自动生成  
	 */ 
	private String fileExtName;
	
	/**  
	 * @Fields field:field: 可选：fileDirectoryPath文件目录路径：自动生成  
	 */ 
	private String fileDirectoryPath;
	
	/**  
	 * @Fields field:field: 可选：params参数传递列表数据key-value结构 
	 */ 
	private JSONObject params;
	
	/**  
	 * @return filePath  
	 */
	public String getFilePath() {
		return filePath;
	}

	/**  
	 * @return fileSize  
	 */
	public Long getFileSize() {
		return this.fileSize;
	}

	/**  
	 * @return fileExtName  
	 */
	public String getFileExtName() {
		return this.fileExtName;
	}

	/**  
	 * @return fileDirectoryPath  
	 */
	public String getFileDirectoryPath() {
		return fileDirectoryPath;
	}
	
	/**  
	 * @return fileName  
	 */
	public String getFileName() {
		return fileName;
	}

	/**  
	 * 创建一个新的实例-构造器 SdkUploadRequest.  
	 * @param filePath  
	 */ 
	public SdkUploadRequest(String filePath) {
		this.filePath = filePath;
		File file = Paths.get(this.filePath).toFile();
		if (StringUtil.isEmpty(this.filePath) ||
				! file.exists()) {
			try {
				throw new SdkUploadError("上传文件失败,请指定目录文件!");
			} catch (SdkUploadError e) {
				e.printStackTrace();
			}
		}
		this.fileSize = file.length();
		this.fileName = file.getName();
		this.fileKeyName = "filename";
		this.fileExtName = file.getPath().substring(file.getPath().lastIndexOf(".")+1,file.getPath().length());
		this.fileDirectoryPath = file.getPath().substring(0,file.getPath().lastIndexOf(File.separator));
	}
	
	/**  
	 * 创建一个新的实例-构造器 SdkUploadRequest.  
	 * @param fileKey 文件参数名称
	 * @param fileValue  文件参数数值
	 */ 
	public SdkUploadRequest(String fileKey ,String fileValue) {
		this.filePath = fileValue;
		File file = Paths.get(this.filePath).toFile();
		if (StringUtil.isEmpty(this.filePath) || 
				! file.exists()) {
			try {
				throw new SdkUploadError("上传文件失败,请指定目录文件!");
			} catch (SdkUploadError e) {
				e.printStackTrace();
			}
		}
		this.fileSize = file.length();
		this.fileName = file.getName();
		this.fileKeyName = fileKey;
		this.fileExtName = file.getPath().substring(file.getPath().lastIndexOf(".")+1,file.getPath().length());
		this.fileDirectoryPath = file.getPath().substring(0,file.getPath().lastIndexOf(File.separator));
	}

	/* (非 Javadoc)  
	 * <p>Title: toString</p>  
	 * <p>Description: </p>  
	 * @return  
	 * @see java.lang.Object#toString()  
	 */  
	@Override
	public String toString() {
		return "SdkUploadRequest [filePath=" + filePath + ", fileSize=" + fileSize + ", fileExtName=" + fileExtName
				+ ", fileDirectoryPath=" + fileDirectoryPath + "]";
	}

	/**  
	 * 创建一个新的实例-构造器 SdkUploadRequest.  
	 * @param filePath
	 * @param params  
	 */ 
	public SdkUploadRequest(String filePath, JSONObject params) {
		super();
		this.filePath = filePath;
		this.params = params;
	}
}
